﻿using AbpApplication.AppServices;
using AbpApplication.Dto;
using AbpApplication.Expand;
using AbpToDomain.CustomEntityInfo;
using AbpToDomain.EntityInfo;
using Azure;
using Flurl.Http;
using Flurl.Util;
using Mesosphere.InPut;
using Microsoft.IdentityModel.Logging;
using Newtonsoft.Json;
using OfficeOpenXml.Drawing.Chart;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Reflection.Metadata;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using wms_wpf.Apiservice;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace AbpApplication.Flurl
{
    public class ToOutSideInterFace : ApplicationService, IToOutSideInterFace
    {

        private readonly ILoginHelper loginHelper;
        private readonly IRepository<lognote> LogNote_res;

        public ToOutSideInterFace(ILoginHelper loginHelper, IRepository<lognote> logNote_res)
        {
            this.loginHelper = loginHelper;
           this.LogNote_res = logNote_res;
        }

        public async Task<bool> moveContainer(string BoxCode, string direction, string slotCode)
        {
            try
            {
                Ess_Input2 input = new Ess_Input2();
                input.direction = direction;
                input.slotCode = slotCode;
                input.containerCode = BoxCode;
                var url = $"http://{GlobalVariable.EssIp}/conveyor/moveContainer";
                var data = input;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseEss Response = JsonConvert.DeserializeObject<ApiResponseEss>(result);
                string res = Response.data.ToString();
                if (Response.msg == "success")
                {
                    lognote lognote = new lognote()
                    {
                        User = "容器流动",
                        Type = "容器流动成功",
                        DateTime = DateTime.Now,
                        Note = $"moveContainer-输送线失败+{BoxCode}+res.msg:{Response.msg}" +
          $"+res.data:{Response.data}+res.code:{Response.code}"
                    };
                    await LogNote_res.InsertAsync(lognote);
                    return true;
                }
                else
                {
                    lognote lognote = new lognote() {
                        User = "容器流动",
                        Type = "容器流动失败",
                        DateTime=DateTime.Now,
                        Note = $"moveContainer-输送线失败+{BoxCode}+res.msg:{Response.msg}" +
                        $"+res.data:{Response.data}+res.code:{Response.code}"
                    };
                    await LogNote_res.InsertAsync(lognote);
                    return false;
             
                }
            }
            catch (Exception ex)
            {

                lognote lognote = new lognote()
                {
                    User = "容器流动",
                    Type = "容器流动失败",
                    DateTime = DateTime.Now,
                    Note = $"moveContainer-输送线报错+{BoxCode}+{ex.ToString().Substring(0, 150)}"
                };
                await LogNote_res.InsertAsync(lognote);
                return false;
            }
        }
        public async Task<bool> sendCommand(string color, string tagcode, string display)
        {

            try
            {
                Ess_Input4 InPut = new Ess_Input4()
                {
                    color = color,
                    tagCode = tagcode,
                    display = display,
                    mode = "LIGHT"
                };
                var data = InPut;
                var url = $"http://{GlobalVariable.EssIp}/ptl/sendCommand";
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseEss Response = JsonConvert.DeserializeObject<ApiResponseEss>(result);
                if (Response.msg == "success")
                {
                    loginHelper.LoginInfoAsync("null", $"sendCommand-亮灯成功+{InPut.tagCode + InPut.display + Response.msg}");
     
                    return true;
                }
                else
                {
                    loginHelper.LoginInfoAsync("null", $"sendCommand-亮灯失败+{InPut.tagCode + InPut.display + Response.msg}");

                    lognote lognote = new lognote()
                    {
                        User = "亮灯",
                        Type = "亮灯报错",
                        DateTime = DateTime.Now,
                        Note = $"sendCommand-亮灯报错+{InPut.tagCode + InPut.display + "res.msg" +
                        Response.msg + "res.code" + Response.code + "res.data" + Response.data}"
                    };
                    await LogNote_res.InsertAsync(lognote);

                    return false;
                }

            }
            catch(Exception ex)
            {


                lognote lognote = new lognote()
                {
                    User = "亮灯",
                    Type = "亮灯报错",
                    DateTime = DateTime.Now,
                    Note = $"sendCommand-亮灯报错+{ex.Message.Substring(0, 150)}"
                };
                await LogNote_res.InsertAsync(lognote);

                return false;
            }




        }
        public async Task<bool> RBTInRep(List<Custom_InPut> InPut)//mes入库通知
        {
            try
            {
                var url = $"http://{GlobalVariable.CustomIp}/WMSWebApi/api/RBT/RBTInRep";
                var data = InPut;
                string result = await url.WithTimeout(2000). PostJsonAsync(data). ReceiveString();
                ApiResponseCustom Response = JsonConvert.DeserializeObject<ApiResponseCustom>(result);
                if (Response.Result == "S")
                {
                    loginHelper.LoginInfoAsync("sss", $"mes入库通知回传成功{InPut[0].BARCODE}-result:{Response.Result}-Response:{Response.Message}");
                    lognote lognote = new lognote() { Type = "mes入库通知回传成功", Note = $"mes入库通知回传成功{InPut[0].BARCODE}-result:{Response.Result}-Response:{Response.Message}", User = "mes", DateTime = DateTime.Now };
                    await LogNote_res.InsertAsync(lognote);
                    return true;
                }
                else
                {
                    loginHelper.LoginInfoAsync("sss", $"mes入库通知回传失败{InPut[0].BARCODE}-result:{Response.Result}-Response:{Response.Message}");
                    lognote lognote = new lognote() { Type = "mes入库通知回传失败", Note = $"mes入库通知回传失败{InPut[0].BARCODE}-result:{Response.Result}-Response:{Response.Message}", User = "mes", DateTime = DateTime.Now };
                    await LogNote_res.InsertAsync(lognote);
                    return false;
                }
        
            }
            catch(Exception ex) 
            {

                loginHelper.LoginInfoAsync("sss", $"mes入库通知回传失败{InPut[0].BARCODE}-result:{ex.ToString()}");
                return false; }

        }
        public async Task<bool> RBTOutRep(Custom_InPut2 InPut)//mes出库通知
        {
            try
            {
                var url = $"http://{GlobalVariable.CustomIp}/WMSWebApi/api/RBT/RBTOutRep";
                var data = InPut;
                string result = await url.WithTimeout(2000). PostJsonAsync(data).ReceiveString();
                ApiResponseCustom Response = JsonConvert.DeserializeObject<ApiResponseCustom>(result);
                if (Response.Result == "S")
                {
                    var JsonTrans = JsonConvert.SerializeObject(InPut);
                    loginHelper.LoginInfoAsync("sss", $"mes关闭通知回传成功-result:{Response.Result}-parmemter:{JsonTrans}");
                    lognote lognote = new lognote() { Type = "mes关闭通知回传成功", Note = $"mes关闭通知回传成功-result:{Response.Result}-parmemter:{JsonTrans}", User = "mes", DateTime = DateTime.Now };
                    await LogNote_res.InsertAsync(lognote);
                    return true;
                }
                else
                {
                    var JsonTrans = JsonConvert.SerializeObject(InPut);
                    loginHelper.LoginInfoAsync("sss", $"mes关闭通知回传失败-result:{Response.Result}-message:{Response.Message}parmemter:{JsonTrans}");
                    lognote lognote = new lognote() { Type = "mes关闭通知回传失败", Note = $"mes关闭通知回传失败-result:{Response.Result}-message:{Response.Message}parmemter:{JsonTrans}", User = "mes", DateTime = DateTime.Now };
                    await LogNote_res.InsertAsync(lognote);
                    return false;
                }
                
            }
            catch(Exception ex) {
                var JsonTrans = JsonConvert.SerializeObject(InPut);
                loginHelper.LoginInfoAsync("sss", $"mes关闭通知回传错误-result:-parmemter:{JsonTrans}-信息：{ex.Message}");
                return false; }

        }
        public async Task<string> CreateTask(Ess_Input input)
        {
            try
            {
                var url = $"http://{GlobalVariable.EssIp}/task/create";
                var data = input;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseEss Response = JsonConvert.DeserializeObject<ApiResponseEss>(result);
                string res = Response.data.ToString();
                if (Response.msg == "success")
                {
                    loginHelper.LoginInfoAsync("sss", $"msg:{Response.msg},res:{res}");
                    return res;
                }
                else
                {
                    return Response.msg;
                }

            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public async Task<string> queryContainer(string BoxCode)//容器查询
        {

            try
            {
                Ess_Input5 input = new Ess_Input5();
                input.containerCodes.Add(BoxCode);
                var url = $"http://{GlobalVariable.EssIp}/container/queryContainer";
                var data = input;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseEss3 Response = JsonConvert.DeserializeObject<ApiResponseEss3>(result);
                if (Response.Msg == "success")
                {

                    ResponseData responseData = JsonConvert.DeserializeObject<ResponseData>(Response.data.ToString());
                    return  responseData.containers[0].positionCode;
                }
                else
                {
                    return "m=";
                }
            
           
            }
            catch(Exception ex) 
            {
                return "m=";
            }
            

        }
        public async Task<bool> QuickAdjust(InPut_CunsumerService6 InPut)//快速校准数量
        {
            try
            {
                var url = $"http://{GlobalVariable.CustomIp}/WMSWebApi/api/TASK/QuickAdjust";
                var data = InPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseCustom2 Response = JsonConvert.DeserializeObject<ApiResponseCustom2>(result);
                if (Response.IsSuccess)
                {
                    return true;
                }
                else
                {
                    var JsonTrans = JsonConvert.SerializeObject(InPut);
                    loginHelper.LoginInfoAsync("sss", $"mes修改回传成功-result:{Response.IsSuccess}-parmemter:{JsonTrans}");
                    return false;
                }

            }
            catch (Exception ex)
            {

                return false;
            }
        }
    }
}
